<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>字符串 | Swift 简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.1.0">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
    

        
    
    
    <link rel="next" href="./07-collection.html" />
    
    
    <link rel="prev" href="./05-loop.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="0.6" data-basepath="." data-revision="Sun Jul 26 2015 06:31:06 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                        <i class="fa fa-check"></i>
                        
                        Swift快速入门
                    </a>
                
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="0.1" data-path="01-playground.html">
            
                
                    <a href="./01-playground.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.1.</b>
                        
                        Playground
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.2" data-path="02-var_and_let.html">
            
                
                    <a href="./02-var_and_let.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.2.</b>
                        
                        变量和常量
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.3" data-path="03-optional.html">
            
                
                    <a href="./03-optional.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.3.</b>
                        
                        Optional可选值
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.4" data-path="04-control_flow.html">
            
                
                    <a href="./04-control_flow.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.4.</b>
                        
                        控制流
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.5" data-path="05-loop.html">
            
                
                    <a href="./05-loop.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.5.</b>
                        
                        循环
                    </a>
                
            
            
        </li>
    
        <li class="chapter active" data-level="0.6" data-path="06-string.html">
            
                
                    <a href="./06-string.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.6.</b>
                        
                        字符串
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.7" data-path="07-collection.html">
            
                
                    <a href="./07-collection.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.7.</b>
                        
                        集合
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.8" data-path="08-func.html">
            
                
                    <a href="./08-func.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.8.</b>
                        
                        函数
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.9" data-path="09-closure.html">
            
                
                    <a href="./09-closure.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.9.</b>
                        
                        闭包
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.10" data-path="10-init_func.html">
            
                
                    <a href="./10-init_func.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.10.</b>
                        
                        构造函数
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.11" data-path="11-lazy.html">
            
                
                    <a href="./11-lazy.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.11.</b>
                        
                        懒加载 &amp; didSet
                    </a>
                
            
            
        </li>
    
        <li class="chapter " data-level="0.12" data-path="12-ats.html">
            
                
                    <a href="./12-ats.html">
                        <i class="fa fa-check"></i>
                        
                            <b>0.12.</b>
                        
                        ATS
                    </a>
                
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Swift 简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="%E5%AD%97%E7%AC%A6%E4%B8%B2">&#x5B57;&#x7B26;&#x4E32;</h1>
<blockquote>
<p>&#x5728; Swift &#x4E2D;&#x7EDD;&#x5927;&#x591A;&#x6570;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x63A8;&#x8350;&#x4F7F;&#x7528; <code>String</code> &#x7C7B;&#x578B;</p>
</blockquote>
<ul>
<li>String &#x662F;&#x4E00;&#x4E2A;&#x7ED3;&#x6784;&#x4F53;&#xFF0C;&#x6027;&#x80FD;&#x66F4;&#x9AD8;<ul>
<li>String &#x76EE;&#x524D;&#x5177;&#x6709;&#x4E86;&#x7EDD;&#x5927;&#x591A;&#x6570; NSString &#x7684;&#x529F;&#x80FD;</li>
<li>String &#x652F;&#x6301;&#x76F4;&#x63A5;&#x904D;&#x5386;</li>
</ul>
</li>
<li>NSString &#x662F;&#x4E00;&#x4E2A; OC &#x5BF9;&#x8C61;&#xFF0C;&#x6027;&#x80FD;&#x7565;&#x5DEE;</li>
<li>Swift &#x63D0;&#x4F9B;&#x4E86; String &#x548C; NSString &#x4E4B;&#x95F4;&#x7684;&#x65E0;&#x7F1D;&#x8F6C;&#x6362;</li>
</ul>
<h2 id="%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%BC%94%E7%BB%83">&#x5B57;&#x7B26;&#x4E32;&#x6F14;&#x7EC3;</h2>
<ul>
<li>&#x904D;&#x5386;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x5B57;&#x7B26;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-keyword">for</span> s <span class="hljs-keyword">in</span> str.characters {
    <span class="hljs-built_in">print</span>(s)
}
</code></pre>
<ul>
<li>&#x5B57;&#x7B26;&#x4E32;&#x957F;&#x5EA6;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-comment">// &#x8FD4;&#x56DE;&#x4EE5;&#x5B57;&#x8282;&#x4E3A;&#x5355;&#x4F4D;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x957F;&#x5EA6;&#xFF0C;&#x4E00;&#x4E2A;&#x4E2D;&#x6587;&#x5360; 3 &#x4E2A;&#x5B57;&#x8282;</span>
<span class="hljs-keyword">let</span> len1 = str.lengthOfBytesUsingEncoding(<span class="hljs-type">NSUTF8StringEncoding</span>)
<span class="hljs-comment">// &#x8FD4;&#x56DE;&#x5B9E;&#x9645;&#x5B57;&#x7B26;&#x7684;&#x4E2A;&#x6570;</span>
<span class="hljs-keyword">let</span> len2 = str.characters.<span class="hljs-built_in">count</span>
<span class="hljs-comment">// &#x8FD4;&#x56DE; utf8 &#x7F16;&#x7801;&#x957F;&#x5EA6;</span>
<span class="hljs-keyword">let</span> len3 = str.utf8.<span class="hljs-built_in">count</span>
</code></pre>
<ul>
<li>&#x5B57;&#x7B26;&#x4E32;&#x62FC;&#x63A5;<ul>
<li>&#x76F4;&#x63A5;&#x5728; &quot;&quot; &#x4E2D;&#x4F7F;&#x7528; <code>\(&#x53D8;&#x91CF;&#x540D;)</code> &#x7684;&#x65B9;&#x5F0F;&#x53EF;&#x4EE5;&#x5FEB;&#x901F;&#x62FC;&#x63A5;&#x5B57;&#x7B26;&#x4E32;</li>
</ul>
</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> str1 = <span class="hljs-string">&quot;Hello&quot;</span>
<span class="hljs-keyword">let</span> str2 = <span class="hljs-string">&quot;World&quot;</span>
<span class="hljs-keyword">let</span> i = <span class="hljs-number">32</span>
str = <span class="hljs-string">&quot;<span class="hljs-subst">\(i)</span> &#x4E2A; &quot;</span> + str1 + <span class="hljs-string">&quot; &quot;</span> + str2
</code></pre>
<blockquote>
<p>&#x6211;&#x548C;&#x6211;&#x7684;&#x5C0F;&#x4F19;&#x4F34;&#x518D;&#x4E5F;&#x4E0D;&#x8981;&#x8003;&#x8651; <code>stringWithFormat</code> &#x4E86; :D</p>
</blockquote>
<ul>
<li>&#x53EF;&#x9009;&#x9879;&#x7684;&#x62FC;&#x63A5;<ul>
<li>&#x5982;&#x679C;&#x53D8;&#x91CF;&#x662F;&#x53EF;&#x9009;&#x9879;&#xFF0C;&#x62FC;&#x63A5;&#x7684;&#x7ED3;&#x679C;&#x4E2D;&#x4F1A;&#x6709; <code>Optional</code></li>
<li>&#x4E3A;&#x4E86;&#x5E94;&#x5BF9;&#x5F3A;&#x884C;&#x89E3;&#x5305;&#x5B58;&#x5728;&#x7684;&#x98CE;&#x9669;&#xFF0C;&#x82F9;&#x679C;&#x63D0;&#x4F9B;&#x4E86; <code>??</code> &#x64CD;&#x4F5C;&#x7B26;</li>
<li><code>??</code> &#x64CD;&#x4F5C;&#x7B26;&#x7528;&#x4E8E;&#x68C0;&#x6D4B;&#x53EF;&#x9009;&#x9879;&#x662F;&#x5426;&#x4E3A; <code>nil</code><ul>
<li>&#x5982;&#x679C;&#x4E0D;&#x662F; <code>nil</code>&#xFF0C;&#x4F7F;&#x7528;&#x5F53;&#x524D;&#x503C;</li>
<li>&#x5982;&#x679C;&#x662F; <code>nil</code>&#xFF0C;&#x4F7F;&#x7528;&#x540E;&#x9762;&#x7684;&#x503C;&#x66FF;&#x4EE3;</li>
</ul>
</li>
</ul>
</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> str1 = <span class="hljs-string">&quot;Hello&quot;</span>
<span class="hljs-keyword">let</span> str2 = <span class="hljs-string">&quot;World&quot;</span>
<span class="hljs-keyword">let</span> i: <span class="hljs-type">Int</span>? = <span class="hljs-number">32</span>
str = <span class="hljs-string">&quot;<span class="hljs-subst">\(i ?? <span class="hljs-number">0</span>)</span> &#x4E2A; &quot;</span> + str1 + <span class="hljs-string">&quot; &quot;</span> + str2
</code></pre>
<ul>
<li>&#x683C;&#x5F0F;&#x5316;&#x5B57;&#x7B26;&#x4E32;<ul>
<li>&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x5B57;&#x7B26;&#x4E32;&#x683C;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>String(format:...)</code> &#x7684;&#x65B9;&#x5F0F;</li>
</ul>
</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> h = <span class="hljs-number">8</span>
<span class="hljs-keyword">let</span> m = <span class="hljs-number">23</span>
<span class="hljs-keyword">let</span> s = <span class="hljs-number">9</span>
<span class="hljs-keyword">let</span> timeString = <span class="hljs-type">String</span>(format: <span class="hljs-string">&quot;%02d:%02d:%02d&quot;</span>, arguments: [h, m, s])
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x540E;&#x9762;&#x7684;&#x53C2;&#x6570;&#x9700;&#x8981;&#x653E;&#x5728;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x4E2D;</p>
</blockquote>
<h2 id="string-range-%E7%9A%84%E7%BB%93%E5%90%88">String &amp; Range &#x7684;&#x7ED3;&#x5408;</h2>
<ul>
<li>&#x5728; Swift &#x4E2D;&#xFF0C;<code>String</code> &#x548C; <code>Range</code>&#x8FDE;&#x7528;&#x65F6;&#xFF0C;&#x8BED;&#x6CD5;&#x7ED3;&#x6784;&#x6BD4;&#x8F83;&#x590D;&#x6742;</li>
<li>&#x5982;&#x679C;&#x4E0D;&#x4E60;&#x60EF; Swift &#x7684;&#x8BED;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210; <code>NSString</code> &#x518D;&#x5904;&#x7406;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> helloString = <span class="hljs-string">&quot;&#x6211;&#x4EEC;&#x4E00;&#x8D77;&#x98DE;&quot;</span>
(helloString <span class="hljs-keyword">as</span> <span class="hljs-type">NSString</span>).substringWithRange(<span class="hljs-type">NSMakeRange</span>(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>))
</code></pre>
<h3 id="%E4%BA%86%E8%A7%A3-range">&#x4E86;&#x89E3; Range</h3>
<ul>
<li>&#x5B9A;&#x4E49; Range</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> badRange = <span class="hljs-number">2</span>...<span class="hljs-number">4</span>
</code></pre>
<blockquote>
<p>&#x5229;&#x7528; <code>Option + Click</code> &#x53EF;&#x4EE5;&#x53D1;&#x73B0; badRange &#x7684;&#x7C7B;&#x578B;&#x65F6; <code>Range&lt;Int&gt;</code></p>
</blockquote>
<ul>
<li>&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x56E0;&#x4E3A;&#x9700;&#x8981;&#x7684;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x662F; <code>Range&lt;Index&gt;</code></li>
</ul>
<pre><code class="lang-swift">helloString.substringWithRange(badRange)
</code></pre>
<ul>
<li>&#x4F7F;&#x7528; <code>advance</code> &#x51FD;&#x6570;&#x80FD;&#x591F;&#x5B9A;&#x4E49; <code>Range&lt;Index&gt;</code></li>
</ul>
<pre><code class="lang-swift"><span class="hljs-keyword">let</span> range = <span class="hljs-built_in">advance</span>(helloString.startIndex, <span class="hljs-number">0</span>)...<span class="hljs-built_in">advance</span>(helloString.endIndex, -<span class="hljs-number">1</span>)
</code></pre>
<ul>
<li>&#x4F7F;&#x7528; <code>range</code> &#x622A;&#x53D6;&#x5B57;&#x4E32;</li>
</ul>
<pre><code class="lang-swift">helloString.substringWithRange(range)
<span class="hljs-comment">//: &#x6709;&#x4E86; Range &#x4E4B;&#x540E;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x65B9;&#x5F0F;&#x7B80;&#x5199;</span>
helloString[range]
</code></pre>
<ul>
<li>&#x83B7;&#x53D6;&#x5236;&#x5B9A;&#x4F4D;&#x7F6E;&#x7684;&#x5B57;&#x7B26;</li>
</ul>
<pre><code class="lang-swift"><span class="hljs-comment">//: &#x6709;&#x4E86; Range &#x4E4B;&#x540E;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x65B9;&#x5F0F;&#x7B80;&#x5199;</span>
helloString[range]

<span class="hljs-comment">//: &#x622A;&#x53D6;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;</span>
helloString[range.startIndex]

<span class="hljs-comment">//: &#x622A;&#x53D6;&#x4EFB;&#x610F;&#x5236;&#x5B9A;&#x4F4D;&#x7F6E;&#x5B57;&#x7B26;</span>
helloString[<span class="hljs-built_in">advance</span>(helloString.startIndex, <span class="hljs-number">3</span>)]
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./05-loop.html" class="navigation navigation-prev " aria-label="Previous page: 循环"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./07-collection.html" class="navigation navigation-next " aria-label="Next page: 集合"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
